<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


$project_dir=$_GET['project_dir'];
$semf_root = "../";

$project_home = $project_dir;

$files = array();
$d = dir($project_home);
// Loop through all of the files:
$i = 0;
while (false !== ($file = $d->read()))
{
	// If the file is not this file, and does not start with a '.' or '~'
	// and does not end in LCK, then store it for later display:
	if ( ($file{0} != '.') &&
		 ($file{0} != '~') &&
		 (substr($file, -3) != 'php') &&
		 is_dir($project_home.$file) )
	{
		if($file == "store")
		{
			continue;
		}
		$files[$i++] = $file;
	}
}


$table_doc = new DOMDocument();
$root = $table_doc->createElement("Tables");
$table_doc->appendChild($root);

$pages_doc = new DOMDocument();
$page_root = $pages_doc->createElement("Pages");
$pages_doc->appendChild($page_root);

foreach($files as $file)
{
	$module_name = $file;
	
	$module_ele = $table_doc->createElement("Module");
	$module_ele->setAttribute("name", $module_name);
	$root->appendChild($module_ele);
	
	$page_module_ele = $pages_doc->createElement("Module");
	$page_module_ele->setAttribute("name", $module_name);
	$page_root->appendChild($page_module_ele);
	
	$module_xml = $project_home.$file."/"."module.xml";
	
	$doc = new DOMDocument();
	$doc->load($module_xml);
	
	if($doc == NULL || $doc->documentElement == NULL)
	{
		exit();
	}
	$table_elements = $doc->documentElement->childNodes;
	
	
	foreach ($table_elements as $table_element)
	{
		if($table_element->nodeType != XML_ELEMENT_NODE)
		{
			continue;
		}
		$sql_text = "";
		$is_interface = $table_element->getAttribute("interface");
		$table_ele = $table_doc-> createElement("Table");
		$module_ele->appendChild($table_ele);
		
		if($is_interface == "yes")
		{
			$table_ele->setAttribute("interface", '1');
		}
		$table_name = $table_element->getAttribute("name");
		$table_ele-> setAttribute("name", $table_name);
		
		
		$all_fields = array();
		$direct_fields = array();
		
		$field_elements = $table_element->childNodes;
		
		foreach($field_elements as $field_element)
		{		
			if($field_element->tagName != "field")
			{
				continue;
			}
			$field_info = array(
					"name"=> $field_element->getAttribute("name"),
					"type"=> $field_element->getAttribute("type"),
					"isarray"=> $field_element->getAttribute("isarray"));
			
			$direct_fields[$field_element->getAttribute("name")] = 1;
			
			$all_fields[count($all_fields)] = $field_info;		
		}
		
		//load all the inherited information..
		$inherited_elements = $table_element->childNodes;
		foreach($inherited_elements as $inherited_element)
		{
			if($inherited_element->tagName != "inherited")
			{
				continue;
			}
			$inherited_table = $inherited_element->nodeValue;
			$pos = strpos($inherited_table, ".");
			$inherited_table_owner_module = substr($inherited_table, 0, $pos);
			$inherited_table_name = trim(substr($inherited_table, $pos+1));
			
			
			$inherited_module_xml = $project_home.trim($inherited_table_owner_module)."/module.xml";
			$inherited_doc = new DOMDocument();
			$inherited_doc->load($inherited_module_xml);
			
			
			
			$inherited_table_elements = $inherited_doc->documentElement->childNodes;
			foreach ($inherited_table_elements as $inherited_table_element)
			{
				if($inherited_table_element->nodeType != XML_ELEMENT_NODE)
				{
					continue;
				}
				if($inherited_table_element->getAttribute("name") == $inherited_table_name)
				{
					$inherited_field_elements = $inherited_table_element->childNodes;
					/*$inherited_field_elements = 
						array_merge($inherited_field_elements, $field_elements);*/
					foreach($inherited_field_elements as $inherited_field_element)
					{			
						if($inherited_field_element->tagName != "field")
						{
							continue;
						}
						$name = $inherited_field_element->getAttribute("name");
						if($direct_fields[$name] == 1)
						{
							continue;
						}
						/*$inherited_table_element-> removeChild($inherited_field_element);
						$table_element->appendChild($inherited_field_element); */
						$field_info = array(
								"name"=> $inherited_field_element->getAttribute("name"),
								"type"=> $inherited_field_element->getAttribute("type"),
								"isarray"=> $inherited_field_element->getAttribute("isarray"),
								"inherited" => 1);
						
						$all_fields[count($all_fields)] = $field_info;
					}
					break;
				}
			}
		}
		
		
		foreach($all_fields as $field_info)
		{
			$field_name = $field_info["name"];
			$field_type = $field_info["type"];
			
			$is_array = $field_info["isarray"];
			$inherited = $field_info["inherited"];
			
			$field_ele = $table_doc->createElement("field");
			$table_ele->appendChild($field_ele);
			
			$field_ele->setAttribute("name", $field_name);
			$field_ele->setAttribute("type", $field_type);
			
			$field_ele->setAttribute("isarray", '1');
			if($inherited  == 1)
			{
				$field_ele->setAttribute("inherited", '1');
			}
		}
	}
		
	//list all the files..
	$d = dir($project_home.$file);
	// Loop through all of the files:
	$i = 0;
	$pages = array();
	while (false !== ($page = $d->read()))
	{
		// If the file is not this file, and does not start with a '.' or '~'
		// and does not end in LCK, then store it for later display:
		if ( ($page{0} != '.') &&
			 ($page{0} != '~') &&
			 (substr($page, -3) == 'xml') )
		{
			if($page == "module.xml")
			{
				continue;
			}
			$pages[$i++] = $page;
		}
	}
	
	foreach($pages as $page)
	{
		$pos = strpos($page, ".xml");
		$page_name = substr($page, 0, $pos);
		$page_ele = $pages_doc->createElement("page");
		$page_ele->setAttribute("name", $page_name);
		$page_module_ele->appendChild($page_ele);
	}
}

echo "<option id=\"table_status_view\">";
echo $table_doc->saveXML($table_doc->documentElement);
echo "</option>";

echo "<option id=\"page_status_view\">";
echo $pages_doc->saveXML($pages_doc->documentElement);
echo "</option>";


?>